package org.hgg.springwork.beans.factory.support;

import org.hgg.springwork.beans.BeansException;
import org.hgg.springwork.beans.factory.BeanFactory;
import org.hgg.springwork.beans.factory.config.BeanDefinition;

/**
 * bean工厂抽象类，实现了BeanFactory接口，提供bean的创建和获取方法
 *
 * @author Jarvis
 * @date 2023/12/16 0:07
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String beanName) throws BeansException {
        Object bean = getSingleton(beanName);
        if (bean != null) {
            return bean;
        }
        BeanDefinition beanDefinition = getBeanDefinition(beanName);
        return createBean(beanName, beanDefinition);
    }

    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition) throws BeansException;

    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;


}
